//
//  NSString+NSStringHelper.m
//  SchoolPlus
//
//  Created by haoyi on 13-7-1.
//  Copyright (c) 2013年 haoyi. All rights reserved.
//

#import "NSString+NSStringHelper.h"
#import "Reachability.h"
@implementation NSString (NSStringHelper)


- (NSString *)URLEncodedString{
    
    NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                            (CFStringRef)self,
                                            (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",
                                            NULL,
                                            kCFStringEncodingUTF8));
    return encodedString;
}

#pragma mark - UTF-8 转成 没有乱码的url
- (NSString *)URLEncodeFromUTF8Str{
    
    //将要添加到URL的字符串进行特殊处理，如果这些字符串含有 &， ？ 这些特殊字符，用“%+ASCII” 代替
    CFStringRef nonAlphaNumValidChars = CFSTR("!’()*+,-./:;=?@_~' ");
    
    NSString *preprocessedString = (NSString *)CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
                                                                                                       (CFStringRef)self,
                                                                                                       CFSTR(""),
                                                                                                       kCFStringEncodingUTF8));
    
    NSString *strResult = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                                (CFStringRef)preprocessedString,
                                                                                                NULL,
                                                                                                nonAlphaNumValidChars,
                                                                                                kCFStringEncodingUTF8));
    
    return strResult;
}

#pragma mark -  中文 转成 没有乱码的url
- (NSString *)URLEncodeFromGB2312Str{
    
    //将要添加到URL的字符串进行特殊处理，如果这些字符串含有 &， ？ 这些特殊字符，用“%+ASCII” 代替
    CFStringRef nonAlphaNumValidChars = CFSTR("![        DISCUZ_CODE_1        ]’()*+,-./:;=?@_~");
    
    NSString *preprocessedString = (NSString *)CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
                                                                                                       (CFStringRef)self,
                                                                                                       CFSTR(""),
                                                                                                       kCFStringEncodingGB_18030_2000));
    
    NSString *strResult = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                                (CFStringRef)preprocessedString,
                                                                                                NULL,
                                                                                                nonAlphaNumValidChars,
                                                                                                kCFStringEncodingGB_18030_2000));

    return strResult;
}

#pragma mark -  MD5加密
- (NSString *)MD5HexDigestStr{
    
    const char *original_str = [self UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(original_str, strlen(original_str), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < 16; i++){
        
        [hash appendFormat:@"%02X", result[i]];
    }
    
    NSString *mdfiveString = [hash lowercaseString];
    
    return mdfiveString;
}

#pragma mark -  账号 包含字母数字下划线
- (BOOL)CheckZhangHao{
    
    if ([self length] == 0) {
        
        return NO;
    }
    
    NSString *regex = @"^\\w{1,6}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:self];
}

#pragma mark -  手机号
- (BOOL)CheckTelNumber{
    
    if ([self length] == 0) {
        
        return NO;
    }
    //1[0-9]{10}
    
    //^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$
    
    //    NSString *regex = @"[0-9]{11}";
    
    NSString *regex = @"^((13[0-9])|(147)|(170)|(15[^4,\\D])|(18[^4,\\D]))\\d{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:self];
}

#pragma mark -  邮箱格式
-(BOOL)CheckEmail{
    
    if ([self length] == 0) {
        
        return NO;
    }
    
    NSString *Regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    
    return [emailTest evaluateWithObject:self];
}

#pragma mark -  是否包含中文
- (BOOL)IsContainedChinese{
    
    for(int i = 0; i < [self length]; i++) {
        
        int a = [self characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff)
            
            return  YES;
    }
    
    return NO;
}

#pragma mark -  有效地址
-(BOOL)CheckAddress{
    
    if ([self length] == 0) {
        
        return NO;
    }
    
    NSString *Regex = @"^[A-Z0-9a-z#\u4e00-\u9fa5]$";
    NSPredicate *addressest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    
    return [addressest evaluateWithObject:self];
}

#pragma mark -  验证非零的正整数
-(BOOL)CheckFeilingzhengzhengshu{
    
    if ([self length] == 0) {
        
        return NO;
    }
    
    NSString *Regex = @"^\\+?[1-9][0-9]*$";
    NSPredicate *addressest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    
    return [addressest evaluateWithObject:self];
}
#pragma mark - 密码格式
-(BOOL)CheckPassword{
    if ([self length] == 0) {
        
        return NO;
    }
    
    NSString *Regex = @"^[a-zA-z0-9]{6,}$";
    NSPredicate *PsdTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    
    return [PsdTest evaluateWithObject:self];
}
-(BOOL)CheckPassword2{
    if ([self length] == 0) {
        
        return NO;
    }
    
    NSString *Regex = @"^[0-9]+$|^[a-zA-Z]+$";
    NSPredicate *PsdTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    
    return [PsdTest evaluateWithObject:self];
}
#pragma mark - 验证金额 整数位最多十位，小数为最多为两位，可以无小数位
-(BOOL)CheckJinE{

    if ([self length] == 0) {
        
        return NO;
    }
    
    NSString *Regex = @"^(([1-9]\\d{0,9})|0)(\\.\\d{1,2})?$";
    NSPredicate *addressest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    
    return [addressest evaluateWithObject:self];
}


#pragma mark -  //土土商户邀请码
- (BOOL)CheckInvitationCode{
    
    if ([self length] == 0) {
        
        return NO;
    }
    
    NSString *regex = @"^[A-Za-z0-9]{6}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:self];
}
-(NSString*)GetCurrntNet
{
    NSString* result;
    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([r currentReachabilityStatus]) {
        case NotReachable:// 没有网络连接
            result=@"no";
            break;
        case ReachableViaWWAN:// 使用3G网络
            result=@"3g";
            break;
        case ReachableViaWiFi:// 使用WiFi网络
            result=@"wifi";
            break;
    }
    return result;
}
//判断文件格式
-(BOOL)checkfileformat{
    if ([self hasSuffix:@".doc"]||[self hasSuffix:@".docx"]||[self hasSuffix:@".xlsx"]||[self hasSuffix:@".xls"]||[self hasSuffix:@".png"]||[self hasSuffix:@".jpg"]||[self hasSuffix:@".ppt"]||[self hasSuffix:@".pptx"]||[self hasSuffix:@".mp3"]||[self hasSuffix:@".pdf"]||[self hasSuffix:@".txt"]||[self hasSuffix:@".mp4"]||[self hasSuffix:@".JPEG"]||[self hasSuffix:@".GIF"]||[self hasSuffix:@".bmp"]||[self hasSuffix:@".DOC"]||[self hasSuffix:@".DOCX"]||[self hasSuffix:@".XLSX"]||[self hasSuffix:@".XLS"]||[self hasSuffix:@".PNG"]||[self hasSuffix:@".JPG"]||[self hasSuffix:@".PPT"]||[self hasSuffix:@".PPTX"]||[self hasSuffix:@".MP3"]||[self hasSuffix:@".PDF"]||[self hasSuffix:@".TXT"]||[self hasSuffix:@".MP4"]||[self hasSuffix:@".jpeg"]||[self hasSuffix:@".gif"]||[self hasSuffix:@".BMP"]||[self hasSuffix:@".flv"]||[self hasSuffix:@".FLV"]||[self hasSuffix:@".asx"]||[self hasSuffix:@".ASX"]||[self hasSuffix:@".asf"]||[self hasSuffix:@".ASF"]||[self hasSuffix:@".mpg"]||[self hasSuffix:@".MPG"]||[self hasSuffix:@".WMV"]||[self hasSuffix:@".wmv"]||[self hasSuffix:@".3gp"]||[self hasSuffix:@".3GP"]||[self hasSuffix:@".MOV"]||[self hasSuffix:@".mov"]||[self hasSuffix:@".avi"]||[self hasSuffix:@".AVI"]) {
        return YES;

    }else{
        return NO;

    }
    
}
-(BOOL)checkfileisvedio{
    if ([self hasSuffix:@".mp4"]||[self hasSuffix:@".MP4"]||[self hasSuffix:@".flv"]||[self hasSuffix:@".FLV"]||[self hasSuffix:@".asx"]||[self hasSuffix:@".ASX"]||[self hasSuffix:@".asf"]||[self hasSuffix:@".ASF"]||[self hasSuffix:@".mpg"]||[self hasSuffix:@".MPG"]||[self hasSuffix:@".WMV"]||[self hasSuffix:@".wmv"]||[self hasSuffix:@".3gp"]||[self hasSuffix:@".3GP"]||[self hasSuffix:@".MOV"]||[self hasSuffix:@".mov"]||[self hasSuffix:@".avi"]||[self hasSuffix:@".AVI"]) {
        return YES;
        
    }else{
        return NO;
        
    }
    
}

-(NSString *)checkfiletype{
    if ([self hasSuffix:@".doc"]||[self hasSuffix:@".docx"]||[self hasSuffix:@".DOCX"]||[self hasSuffix:@".DOC"]) {
        return @"doctip.png";
        
    }
    else if ([self hasSuffix:@".xlsx"]||[self hasSuffix:@".xls"]||[self hasSuffix:@".XLS"]||[self hasSuffix:@".XLSX"]) {
        return @"excelltip";

        
    }
    else if ([self hasSuffix:@".png"]||[self hasSuffix:@".PNG"]||[self hasSuffix:@".jpg"]||[self hasSuffix:@".JPG"]||[self hasSuffix:@".gif"]||[self hasSuffix:@".GIF"]) {
        return @"jpgtip";

    }
    else if ([self hasSuffix:@".pptx"]||[self hasSuffix:@".ppt"]||[self hasSuffix:@".PPT"]||[self hasSuffix:@".PPTX"]) {
        return @"ppttip";

    }
   
    else if ([self hasSuffix:@".mp3"]||[self hasSuffix:@".MP3"]) {
        return @"mp3tip";

    }
    else if ([self hasSuffix:@".pdf"]||[self hasSuffix:@".PDF"]) {
        return @"pdftip";

    }
    else if ([self hasSuffix:@".rar"]||[self hasSuffix:@".RAR"]) {
        return @"rartip";

    }
    else if ([self hasSuffix:@".zip"]||[self hasSuffix:@".ZIP"]) {
        return @"ziptip";
        
    }
    else if ([self hasSuffix:@".txt"]||[self hasSuffix:@".TXT"]) {
        return @"txttip";

    }
    else if ([self hasSuffix:@".rmvb"]||[self hasSuffix:@".RMVB"]||[self hasSuffix:@".avi"]||[self hasSuffix:@".AVI"]||[self hasSuffix:@".mp4"]||[self hasSuffix:@".MP4"]||[self hasSuffix:@".flv"]||[self hasSuffix:@".FLV"]) {
        return @"rmvbtip";

    }else{
        return @"othertip";

    }

}
- (NSString *)stringByDecodingURLFormat
{
    NSString *result = [(NSString *)self stringByReplacingOccurrencesOfString:@"+" withString:@" "];
    result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return result;
}
@end
